﻿using System;
using System.Collections.Generic;
using OS.Infrastructure.Domain.UnitOfWork;
using TaskKernel.Configuration;
using TaskKernel.DTO;
using TaskKernel.Domain;
using TaskKernel.Domain.Interface;
using TaskKernel.Domain.Services.Action;

namespace TaskWebApplicationUI.PresenterLayer
{
    public class ArchiveTaskPresenter : AbstractPresenter<IArchiveTaskView>
    {
        private readonly IQueryAccount _queryAccount;
        private readonly IDepartmentService _departmentService;
        private readonly IUnitOfWorkFactory _uow;
        private readonly IQueryDepartment _queryDepartment;
        private Account _currentAccount;
        private Department _currentDepartment;

        public ArchiveTaskPresenter(IQueryDepartment queryDepartment, IQueryAccount queryAccount,
                                    IDepartmentService departmentService, IUnitOfWorkFactory uow)
        {
            _queryDepartment = queryDepartment;
            _queryAccount = queryAccount;
            _departmentService = departmentService;
            _uow = uow;
        }
        private Account CurrentAccount
        {
            get
            {
                if (_currentAccount != null)
                    return _currentAccount;
                if (!String.IsNullOrEmpty(View.LogonUser))
                {
                    _currentAccount = _queryAccount.Get(View.LogonUser);
                    if (_currentAccount != null)
                        return _currentAccount;
                }
                throw new NotImplementedException("Don't know who are you...");
            }
        }

        private Department CurrentDepartment
        {
            get
            {
                if (_currentDepartment != null)
                    return _currentDepartment;
                if (!String.IsNullOrEmpty(View.WebPath))
                {
                    _currentDepartment = _queryDepartment.GetDepartmentByWebPath(View.WebPath);
                    if (_currentDepartment != null) return _currentDepartment;
                    if (_queryDepartment == null)
                        throw new NotImplementedException("queryDepartment is null");
                }
                throw new NotImplementedException("Don't know where are you...");
            }
        }

        private void SetDepartmentName()
        {
            View.DepartmentName = CurrentDepartment.DepartmentName;
        }
        public void OnViewInitialized()
        {
            using (var z = _uow.Create()){
                SetDepartmentName();
                LoadExecutorsList();
                LoadDepartments();
            }
        }

        private void LoadDepartments()
        {
            var departments = _queryDepartment.GetAllDepartments();
            View.Departments = departments.ToView();
            var accounts = _queryAccount.GetAllNotFiredAccounts();
            View.Participants = accounts.ToView();
            View.Owners = accounts.ToView();
            View.Executors = accounts.ToView();
        }

        public virtual void OnSearch()
        {
            using (var z = _uow.Create())
            {

                var modelSearch = View.SearchModel;
                var tasks = modelSearch.InProgressStatus
                                ? _departmentService.GetAllNotArchiveTasksBySearchModel(CurrentAccount, modelSearch)
                                : _departmentService.GetAllArchiveTasksBySearchModel(CurrentAccount, modelSearch);
                if (null != tasks)
                {
                    var executorsModels = new List<ExecutorsModel>
                                          {
                                              tasks.ConvertToExecutorsModelItemArchiveTaskPresenter(TaskKernelResources.Archive,
                                                                                                    CurrentAccount)
                                          };
                    View.Tasks = executorsModels;
                }
            }

        }

        private void LoadExecutorsList()
        {
            var tasks = _departmentService.GetArchiveTasksByAccount(CurrentAccount, CurrentDepartment, 100);
            if (null != tasks)
            {
                var executorsModels = new List<ExecutorsModel>
                                          {
                                              tasks.ConvertToExecutorsModelItemArchiveTaskPresenter(TaskKernelResources.Archive,
                                                                                                    CurrentAccount)
                                          };
                View.Tasks = executorsModels;
            }
        }
    }
}